package com.zhongjian.technologycal.mall.adapter;

import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.zhongjian.technologycal.mall.R;

import java.util.List;
import java.util.Map;

/**
 * Created by 刘少帅 on 2017/2/28.
 * Email:1771050446@qq.com
 */

public class AddressAdminAdapter extends BaseQuickAdapter<Map<String,String>,BaseViewHolder> {
    private final String token;
    private int mCurrentCb=0;
    private boolean isHas;
    public AddressAdminAdapter(int layoutResId, List<Map<String, String>> data, String token) {
        super(layoutResId, data);
        this.token=token;
    }
    public void setIsHasDefaultAddress(boolean mcb){
            this.isHas=mcb;
    }
    @Override
    protected void convert(BaseViewHolder helper, Map<String, String> item) {
        if (!isHas){
            mCurrentCb=-1;
        }
        //LogUtils.i(JSON.toJSONString(item));
        int position = helper.getLayoutPosition();
        CheckBox cbDefault = helper.getView(R.id.cb_item_addressadmin_default);
        //首位的默认地址
        setDefaultAddress(cbDefault,position,item);
        //赋值
        initInterface(helper,item);
            //添加删除和编辑的监听
        helper
        .addOnClickListener(R.id.tv_item_addressadmin_edit)
        .addOnClickListener(R.id.tv_item_addressadmin_delete);
        //对两个图片设置大小
        setTwoIcon(helper);


    }
//
    private void setTwoIcon(BaseViewHolder helper) {

        TextView tvEdit = helper.getView(R.id.tv_item_addressadmin_edit);

        TextView tvDelete = helper.getView(R.id.tv_item_addressadmin_delete);
    }

    private void initInterface(BaseViewHolder helper, Map<String, String> item) {
        helper.setText(R.id.tv_item_addressadmin_receiver,"收货人 : "+item.get("name"));
        helper.setText(R.id.tv_item_addressadmin_phone,item.get("phone"));
        helper.setText(R.id.tv_item_addressadmin_detail,item.get("detailAddress"));
    }

    private void setDefaultAddress(CheckBox cbDefault, final int position
    , final Map<String, String> item) {
       if (position==mCurrentCb){
           cbDefault.setChecked(true);
       }else {
           cbDefault.setChecked(false);
       }

        cbDefault.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //将设置的默认地址发送给后台
                String id = item.get("id");
                mCurrentCb=position;
                notifyDataSetChanged();
                setDefaultAddressAndUploadData(id);
            }
        });
    }

    private void setDefaultAddressAndUploadData(String id) {
//        OkHttpManger.getInstance().getAsync(Constant.url
//                +"SetDefaultAddress?"+"token="+token+"&id="+id,
//                new OKHttpUICallback.ResultCallback<AppBack>() {
//                    @Override
//                    public void onSuccess(AppBack result) {
//                        if (result.getStatus()==0) {
//                            //成功就成功了
//                            ((Activity)mContext).setResult(Activity.RESULT_OK);
//                            //((Activity) mContext).finish();
//                        }else {
//                            Toast.makeText(mContext, result.getResult().toString(), Toast.LENGTH_SHORT).show();
//                        }
//                    }
//
//                    @Override
//                    public void onError(Call call, Exception e) {
//                        Toast.makeText(mContext, mContext.getResources().getString(R.string.network_anomalies), Toast.LENGTH_SHORT).show();
//                    }
//                });

    }
}
